﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;
using System.IO;

namespace Common
{
    public class Encryption
    {
        static string encryptKey = "R%t$&!@7";
        /// <summary>
        /// 得到字符串的MD5加密
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <returns></returns>
        public static string GetMD5(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
        }

        /// <summary>
        /// 得到字符串的SHA1加密
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <returns></returns>
        public static string GetSHA1(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "sha1");
        }



        #region DES

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt"></param>
        /// <returns></returns>
        public static string DESEncrypt(string pToEncrypt)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
            provider.Key = Encoding.ASCII.GetBytes(encryptKey);
            provider.IV = Encoding.ASCII.GetBytes(encryptKey);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            return builder.ToString();
        }


        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <returns></returns>
        public static string DESDecrypt(string pToDecrypt)
        {
            try
            {
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                byte[] buffer = new byte[pToDecrypt.Length / 2];
                for (int i = 0; i < (pToDecrypt.Length / 2); i++)
                {
                    int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
                    buffer[i] = (byte)num2;
                }
                provider.Key = Encoding.ASCII.GetBytes(encryptKey);
                provider.IV = Encoding.ASCII.GetBytes(encryptKey);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                StringBuilder builder = new StringBuilder();
                return Encoding.Default.GetString(stream.ToArray());
            }
            catch
            {
                return "";
            }
        }


        #endregion
    }
}
